package com.gujianjian.datasource;

import com.gujianjian.datasource.entity.SysUser;
import com.gujianjian.datasource.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaohe
 */
@MapperScan("com.gujianjian.datasource.mapper")
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@Import({DynamicDataSourceConfig.class})
@RestController
@Slf4j
public class SpringBootDynamicDataSourceApplication {
    @Autowired
    private SysUserService userService;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDynamicDataSourceApplication.class, args);
    }

    @GetMapping("/test")
    public String test(){
        SysUser user = userService.findUserByFirstDb(1);
        log.info("第一个数据库 :{}",user.toString());
        SysUser user2 = userService.findUserBySecondDb(1);
        log.info("第二个数据库 :{}",user2.toString());
        return "ok";
    }
}
